package com.molichuxing.services.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.business.dto.request.create.MessageCenterReqDto;
import com.molichuxing.services.business.dto.request.create.MessageContentReqDto;
import com.molichuxing.services.business.dto.response.MessageContentResDto;
import com.molichuxing.services.business.dto.response.MessageResDto;
import com.molichuxing.services.business.service.MessageBizService;
import com.molichuxing.services.business.service.RegionBizService;
import com.molichuxing.services.business.service.SubCompanyBizService;
import com.molichuxing.services.infrastructure.bean.MessageCenter;
import com.molichuxing.services.infrastructure.bean.MessageContent;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.MessageCenterMapper;
import com.molichuxing.services.infrastructure.dao.MessageContentMapper;
import com.molichuxing.services.infrastructure.dto.response.RegionCityDto;
import com.molichuxing.services.infrastructure.dto.response.RegionDto;
import com.molichuxing.services.infrastructure.dto.response.RegionSubcompanyDto;
import com.molichuxing.services.infrastructure.dto.response.SubcompanyDto;
import com.molichuxing.services.infrastructure.service.RegionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 消息 服务实现类
 * </p>
 *
 * @author zoumingyu
 * @since 2019-07-28
 */
@Service("messageBizService")
public class MessageBizServiceImpl implements MessageBizService {

	private static final Logger logger = LoggerFactory.getLogger(MessageBizServiceImpl.class);

	@Resource
	private MessageCenterMapper messageCenterMapper;

	@Resource
	private MessageContentMapper messageContentMapper;

	@Override
	public boolean insertMessage(MessageCenterReqDto messageReqDto) {
		boolean flag=false;
		int result=0;
		MessageCenter messageCenter=new MessageCenter();
		BeanUtils.copyProperties(messageReqDto,messageCenter);
		messageCenter.setGmtCreate(LocalDateTime.now());
		messageCenter.setStatus(0);
		//插入消息中心记录
		result+= messageCenterMapper.insertSelective(messageCenter);
		if(!CollectionUtils.isEmpty(messageReqDto.getMessageContentReqDtoList())){
			List<MessageContentReqDto> messageContentReqDtos=messageReqDto.getMessageContentReqDtoList();
			for (MessageContentReqDto messageContentReqDto:messageContentReqDtos){
				MessageContent messageContent=new MessageContent();
				BeanUtils.copyProperties(messageContentReqDto,messageContent);
				messageContent.setCenterId(messageCenter.getId());
				messageContent.setSequence(1);
				//插入消息内容记录
				result+=messageContentMapper.insertSelective(messageContent);
			}
		}
		if(result>1){
			flag=true;
		}
		return flag;
	}

	@Override
	public Paged<MessageResDto> selectMessagesByUserId(Integer userId, Integer pageNum, Integer pageSize) {
		//根据用户id查询用户所有消息
		List<MessageCenter> messageCenters2=messageCenterMapper.getMessageList(userId, null);
		//更新消息中心为已读状态
		if(!CollectionUtils.isEmpty(messageCenters2)){
			for (MessageCenter messageCenter:messageCenters2){
				messageCenter.setStatus(1);
				messageCenterMapper.updateStatus(messageCenter.getId(), 1);
			}
		}
		Paged<MessageResDto> pageResult = null;
		PageHelper.startPage(null==pageNum?1:pageNum, null==pageSize?10:pageSize);
		//根据用户id查询消息中心记录
		List<MessageResDto> messageResDtos=null;

		Integer total =messageCenterMapper.getMessageListCount(userId, null);
		List<MessageCenter>  result = messageCenterMapper.getMessageListByUserId(userId,null,pageNum,pageSize);
		pageResult = new Paged<>();
		pageResult.setTotal(total);

		if(!CollectionUtils.isEmpty(result)){
			messageResDtos=new ArrayList<>();
			for (MessageCenter messageCenter:result){
				MessageResDto messageResDto=new MessageResDto();
				BeanUtils.copyProperties(messageCenter,messageResDto);
				switch (messageResDto.getType()){
					case  1:
						messageResDto.setTypeName("首付成功");
						break;
					case 2:
						messageResDto.setTypeName("提车成功");
						break;
					case 3:
						messageResDto.setTypeName("还款成功");
						break;
					case 4:
						messageResDto.setTypeName("还款提醒");
						break;
					case 5:
						messageResDto.setTypeName("还款逾期");
						break;
					default:
						messageResDto.setTypeName("未知");
				}
				messageResDto.setStatusName(0==messageResDto.getStatus()?"未读":"已读");

				//查询该消息中心记录的详细消息内容
				List<MessageContent> messageContents=messageContentMapper.getMessageContentByCenterId(messageCenter.getId());
				if(!CollectionUtils.isEmpty(messageContents)){
					List<MessageContentResDto> contentResDtos=new ArrayList<>();
					for (MessageContent messageContent:messageContents){
						MessageContentResDto messageContentResDto=new MessageContentResDto();
						BeanUtils.copyProperties(messageContent,messageContentResDto);
						contentResDtos.add(messageContentResDto);
					}
					messageResDto.setMessageContentResDtoList(contentResDtos);
				}
				messageResDtos.add(messageResDto);

				//更新消息中心为已读状态
				messageCenter.setStatus(1);
				messageCenterMapper.updateStatus(messageCenter.getId(), 1);
			}
		}
		pageResult.setList(messageResDtos);
		return pageResult;
	}

	@Override
	public int selectMessagesListByUserId(Integer userId) {
		int result = 0;
		//根据用户id查询用户所有消息
		List<MessageCenter> messageCenters=messageCenterMapper.getMessageList(userId, 0);
		if((null != messageCenters)) {
			result = messageCenters.size();
		}
		return result;
	}

	@Override
	public Map<String, Object> selectMessagesUnReadByUserId(Integer userId) {
		Map<String,Object> map=new HashMap<>();
		List<MessageCenter> messageCenters=messageCenterMapper.getMessageList(userId, 0);
		if(!CollectionUtils.isEmpty(messageCenters)){
			map.put("result",1);
			map.put("data",messageCenters);
			map.put("message","存在未读消息!");
		}else {
			map.put("result",0);
			map.put("message","不存在未读消息！");
		}
		return map;
	}
}
